package prototype;

import java.io.*;
import java.util.ArrayList;

/**
 * @author : DuJiabao
 * @Date : 2021/3/2 9:40
 * @Project : design-pattern
 * @File : null.java
 * @Desc :
 */
public class DeepClone2 implements Serializable {
  public ArrayList arrayList;

  public DeepClone2() {
  }

  public DeepClone2(ArrayList arrayList) {
    this.arrayList = arrayList;
  }

  public DeepClone2 deepClone() {
    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    ByteArrayInputStream bis = null;
    ObjectInputStream ois = null;

    try {
      bos = new ByteArrayOutputStream();
      oos = new ObjectOutputStream(bos);

      oos.writeObject(this);

      bis = new ByteArrayInputStream(bos.toByteArray());
      ois = new ObjectInputStream(bis);
      DeepClone2 clone = (DeepClone2) ois.readObject();
      return clone;
    } catch (IOException | ClassNotFoundException e) {
      e.printStackTrace();
      return null;
    } finally {
      try {
        if (bos != null) bos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (oos != null) oos.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (bis != null) bis.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (ois != null) ois.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
